<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class Cases extends Controller
{
    use \App\Traits\CaseTrait, \App\Traits\CacheTrait, \App\Traits\TechTrait, \App\Traits\SessionTrait;

    public function caseList(Request $request)
    {
        if ($request->method() == 'POST') {
            $caseName = $request->input('name', '');
            $areaId = $request->input('area', -1);
            $status = $request->input('status', -1);
            $pageNumber = $request->input('page', 1);
        } else {
            $caseName = '';
            $areaId = -1;
            $status = -1;
            $pageNumber = 1;
        }
        $pageLimit = 10;
        $postData = [
            'company_id' => $this->sessionReturnCompanyId(),
            'case_name' => $caseName,
            'case_address_id' => $areaId,
            'status' => $status,
            'page_number' => $pageNumber,
            'page_limit' => $pageLimit,
        ];
        $result = $this->getCaseList(parent::CODE_GET_COMPANY_CASE_LIS, $postData);
        $result = $result ? $result : [];
        $provinceList = $this->cacheGetProvinceList();
        if ($result) {
            $viewData = [
                'pageCount' => $result['pageCount'],
                'dataList' => $result['dataList'],
                'currentPage' => $pageNumber,
                'select_info' => 'case',
                'province_list' => $provinceList,
                'case_type_list' => config('constant.case_type_list'),
                'case_check_status' => config('constant.device_check_status'),
            ];

            return ($request->method() == 'POST') ? view('admin/case/case_list_post', $viewData) : view('admin/case/case_list', $viewData);
        }

    }

    public function addCase(Request $request)
    {
        $type = $request->input('type');
        if ($type == 2) {
            return $this->_zhCaseView();
        } else {
            return $this->_techCaseView();
        }

    }

    private function _techCaseView()
    {
        $provinceList = $this->cacheGetProvinceList();
        $otherDicList = $this->cacheGetOtherDicList();
        $domainList = $otherDicList[config('constant.domain_dic')][0];
        $domainList2 = $otherDicList[config('constant.domain_dic')][$domainList[0]['id']];
        $viewData = [
            'select_info' => 'case',
            'province_list' => $provinceList,
            'domain_list' => $domainList,
            'domain_list2' => $domainList2,
        ];
        return view('admin/case/add_tech_case', $viewData);
    }

    private function _zhCaseView()
    {
        $provinceList = $this->cacheGetProvinceList();
        $otherDicList = $this->cacheGetOtherDicList();
        $zhDomainList = $otherDicList[config('constant.domain2_dic')][0];
        $viewData = [
            'select_info' => 'case',
            'province_list' => $provinceList,
            'zh_domain_list' => $zhDomainList,
        ];
        return view('admin/case/add_zh_case', $viewData);
    }

    public function editCase(Request $request)
    {
        $caseId = $request->input('id');
        $postData = [
            'id' => $caseId,
            'company_id' => $this->sessionReturnCompanyId(),
        ];

        $result = $this->getCompanyCaseInfo(parent::CODE_GET_COMPANY_CASE_INFO, $postData);
        if ($result) {
            //技术案例
            if ($result['case_type'] == 1) {
                $provinceList = $this->cacheGetProvinceList();
                $otherDicList = $this->cacheGetOtherDicList();
                $otherNameList = $this->cacheGetOtherDicNameList();
                $domainList = $otherDicList[config('constant.domain_dic')][0];
                $domainList2 = $otherDicList[config('constant.domain_dic')][$domainList[0]['id']];
                $selectDomainList = explode(',', $result['case_domain_id']);

                $postData2 = [
                    'id' => $result['tech_id'],
                    'company_id' => $this->sessionReturnCompanyId(),
                ];
                $result2 = $this->getTechInfo(parent::CODE_GET_TECH_INFO, $postData2);

                $techName = $result2['tech_name'];
                $viewData = [
                    'select_info' => 'case',
                    'province_list' => $provinceList,
                    'domain_list' => $domainList,
                    'domain_list2' => $domainList2,
                    'dataInfo' => $result,
                    'selectDomainList' => $selectDomainList,
                    'getNameList' => $otherNameList,
                    'tech_id' => $result['tech_id'],
                    'tech_name' => $techName,
                    'case_id' => $caseId,
                ];
                return view('admin/case/edit_tech_case', $viewData);

            }
            //综合案例
            else if ($result['case_type'] == 2) {
                $provinceList = $this->cacheGetProvinceList();
                $otherDicList = $this->cacheGetOtherDicList();
                $zhDomainList = $otherDicList[config('constant.domain2_dic')][0];
                $viewData = [
                    'select_info' => 'case',
                    'province_list' => $provinceList,
                    'zh_domain_list' => $zhDomainList,
                    'dataInfo' => $result,
                    'case_id' => $caseId,
                ];
                return view('admin/case/edit_zh_case', $viewData);
            }

        }

    }

    public function caseView(Request $request)
    {
        $id = $request->input('id');

        $postData = [
            'id' => $id,
            'company_id' => $this->sessionReturnCompanyId(),
        ];

        $result = $this->getCompanyCaseInfo(parent::CODE_GET_COMPANY_CASE_INFO, $postData);

        if ($result) {
            $getAreaNameList = $this->getAreaNameList();
            $getOtherNameList = $this->cacheGetOtherDicNameList();
            if ($result['case_type'] == 1) {
                $selectDomainList = explode(',', $result['case_domain_id']);
                $viewData = [
                    'name' => $result['case_name'],
                    'area_name' => $getAreaNameList[$result['case_address_id']]['name'],
                    'img_url' => $result['case_image'][0]['url'],
                    'content' => $result['case_content'],
                    'selectDomainList' => $selectDomainList,
                    'getNameList' => $getOtherNameList,
                    'select_info' => 'case',
                ];
                return view('admin/case/view_tech_case', $viewData);
            } else if ($result['case_type'] == 2) {
                $caseDomainName = $getOtherNameList[$result['case_domain_id']]['name'];
                $viewData = [
                    'name' => $result['case_name'],
                    'area_name' => $getAreaNameList[$result['case_address_id']]['name'],
                    'img_url' => $result['case_image'][0]['url'],
                    'content' => $result['case_content'],
                    'case_domain_name' => $caseDomainName,
                    'select_info' => 'case',
                ];
                return view('admin/case/view_zh_case', $viewData);
            }

        }
    }

    public function techChose(Request $request)
    {
        $postData = [
            'company_id' => $this->sessionReturnCompanyId(),
            'status' => -1, //审核通过的
            'tech_name' => '',
            'domain2' => -1,
            'page_number' => 1,
            'page_limit' => 100,
        ];
        $result = $this->getTechList(parent::CODE_GET_TECH_LIST, $postData);
        if ($result) {
            $viewData = [
                'dataList' => $result['dataList'],
            ];
            return view('admin/case/tech_chose', $viewData);
        }
    }

}
